<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use App\Models\Machine;

class MachineController extends Controller
{
    public function getProductsByMachine(Machine $machine)
    {
        $products = $machine->products;
        return response()->json(['code' => 1, 'data' => $products]);
    }

    public function getCurrentMachine()
    {
        $machine = session('machine');

        if (! $machine) {
            return response()->json(['code' => 1001, 'message' => '用户未扫机器码'], 400);
        }

        $machine->load('products', 'categories');
        $machine->products->map(function ($item) {
            $item->type = explode(';', trim($item->type, ';'));
            $item->skus->map(function ($item2) {
                $item2->type = json_decode($item2->type);
                return $item2;
            });
            return $item;
        });
        return response()->json(['code' => 1, 'data' => $machine]);
    }
}
